﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Text;
using System.Xml;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using EQT.Model;

namespace EQT.Dal
{
    public partial class DaNotice
    {

        public DataSet GetNoticeRecords(string condition, int PageSize, int PageIndex, out int TotalCount)
        {

            string strGetNoticeRecords = "select * from V_Notice ";

            if (!string.IsNullOrEmpty(condition))
            {
                strGetNoticeRecords = strGetNoticeRecords + condition;
            }

            try
            {
                return DaCommon.GetPaginationData(strGetNoticeRecords, PageSize, PageIndex, " Create_Time desc", out TotalCount);

            }
            catch (DbException ex)
            {
                HandleDbException(ex);
            }
            TotalCount = 0;
            return new DataSet();
        }
        public DataSet GetNoticeRecords(string condition)
        {
            string strGetNoticeRecords = "select * from V_Notice ";

            if (!string.IsNullOrEmpty(condition))
            {
                strGetNoticeRecords = strGetNoticeRecords + condition;
            }

            try
            {
                return db.ExecuteDataSet(CommandType.Text,strGetNoticeRecords);

            }
            catch (DbException ex)
            {
                HandleDbException(ex);
            }
            return new DataSet();
        }



        /// <summary>
        /// 查找某用户已读或未读或全部消息。
        /// </summary>
        /// <param name="UID"></param>
        /// <param name="status"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="TotalCount"></param>
        /// <returns></returns>
        public DataSet GetMyNoticeRecords(string UID, int?  status, int PageSize, int PageIndex, out int TotalCount)
        {

            string strGetMyNoticeRecords = "select * from V_Notice_Receive  where UID='" + UID + (status!=null?"' and Rec_Status=" + status.ToString():"'");
            try
            {
                return DaCommon.GetPaginationData(strGetMyNoticeRecords, PageSize, PageIndex, "Rec_Status asc,Rec_Time desc", out TotalCount);

            }
            catch (DbException ex)
            {
                HandleDbException(ex);
            }
            TotalCount = 0;
            return new DataSet();
        }

        /// <summary>
        /// 查找Top5未读消息。
        /// </summary>
        /// <param name="UID"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public DataSet GetMyNotReadNotice(string UID, int count)
        {

            string strGetMyNotReadNotice = "select top " + count.ToString() + "* from V_Notice_Receive where UID='" + UID + "' and Rec_Status=0 order by Rec_Time desc";

            try
            {
                return db.ExecuteDataSet(CommandType.Text, strGetMyNotReadNotice);

            }
            catch (DbException ex)
            {
                HandleDbException(ex);
            }
            return new DataSet();
        }

        /// <summary>
        /// 创建公告,并写到接收记录表中。
        /// </summary>
        /// <param name="mod"></param>
        /// <param name="users"></param>
        /// <returns></returns>
        public bool CreateNotice(MoNotice mod, List<string> users)
        {
            bool rc = false;
            DaNoticeReceiver dalrec = new DaNoticeReceiver();
            using (DbConnection cnn = db.CreateConnection())
            {
                DbTransaction trans = null;
                try
                {
                    cnn.Open();
                    using (trans = cnn.BeginTransaction())
                    {
                        this.Add(mod,trans);
                        for (int i = 0; i < users.Count; i++)
                        {
                            MoNoticeReceiver modrec = new MoNoticeReceiver();
                            modrec.Notice_Id = mod.Notice_Id;
                            modrec.Rec_Status = 0;
                            modrec.Rec_Time = DateTime.Now;
                            modrec.Uid = users[i];

                            dalrec.CreateNoticeReceive(modrec, trans);
                        }
                        trans.Commit();
                        rc = true;
                        cnn.Close();
                    }
                }
                catch (DbException ex)
                {
                    if (trans != null) trans.Rollback();
                    HandleDbException(ex);
                }
            }
            return rc;
        }

        /// <summary>
        /// 给某人toUser发送一个定向消息
        /// </summary>
        /// <param name="mod"></param>
        /// <param name="toUser"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public bool CreateNotice(MoNotice mod, string toUser)
        {           
            bool rc = false;
            DaNoticeReceiver dalrec = new DaNoticeReceiver();
            using (DbConnection cnn = db.CreateConnection())
            {
                DbTransaction trans = null;
                try
                {
                    cnn.Open();
                    using (trans = cnn.BeginTransaction())
                    {
                        this.Add(mod, trans);//添加通知
                        MoNoticeReceiver modrec = new MoNoticeReceiver();
                        modrec.Notice_Id = mod.Notice_Id;
                        modrec.Rec_Status = 0;
                        modrec.Rec_Time = DateTime.Now;
                        modrec.Uid = toUser;
                        dalrec.CreateNoticeReceive(modrec, trans);
                        trans.Commit();
                        rc = true;
                       
                    }
                    cnn.Close();
                }
                catch (DbException ex)
                {
                    if (trans != null) trans.Rollback();
                    HandleDbException(ex);
                }
            }
            return rc;
        }

        /// <summary>
        /// 删除公告，同时删除接收表相应的接收记录。
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public bool DeleteNotice(string p)
        {
            string sql = "DELETE FROM NOTICE_RECEIVER WHERE  NOTICE_ID = '"+p+"'";
            bool rc = false;
            
            using (DbConnection cnn = db.CreateConnection())
            {
                DbTransaction trans = null;
                try
                {
                    cnn.Open();
                    using (trans = cnn.BeginTransaction())
                    {
                        db.ExecuteNonQuery(trans, CommandType.Text, sql);
                        this.Delete(p,trans);
                        trans.Commit();
                        rc = true;
                        cnn.Close();
                    }
                }
                catch (DbException ex)
                {
                    if (trans != null) trans.Rollback();
                    HandleDbException(ex);
                }
            }
            return rc;
        }

        /// <summary>
        /// 读取一条公告信息，并设置成以读状态。
        /// </summary>
        /// <param name="strRecID"></param>
        /// <returns></returns>
        public DataSet GetNoticeAndSetReaded(string strRecID)
        {
            string sql = "select * from V_Notice_Receive WHERE  rec_id = '" + strRecID + "'";
            string setRead = "update Notice_Receiver set Rec_Status=1 where rec_id='" + strRecID + "'";
            DataSet ds = new DataSet();

            using (DbConnection cnn = db.CreateConnection())
            {
                DbTransaction trans = null;
                try
                {
                    cnn.Open();
                    using (trans = cnn.BeginTransaction())
                    {
                        ds = db.ExecuteDataSet(trans, CommandType.Text, sql);

                        db.ExecuteNonQuery(trans, CommandType.Text, setRead);
                        
                        trans.Commit();
                        cnn.Close();
                    }
                }
                catch (DbException ex)
                {
                    if (trans != null) trans.Rollback();
                    HandleDbException(ex);
                }
            }
            return ds;
        }

        /// <summary>
        /// 编辑公告信息.
        /// </summary>
        /// <param name="mod"></param>
        /// <param name="users"></param>
        /// <returns></returns>
        public bool EditNoticeSave(MoNotice mod, List<string> users)
        {
            bool rc = false;
            DaNoticeReceiver dalrec = new DaNoticeReceiver();
            using (DbConnection cnn = db.CreateConnection())
            {
                DbTransaction trans = null;
                try
                {
                    cnn.Open();
                    using (trans = cnn.BeginTransaction())
                    {
                        this.Update(mod, trans);
                        string sqlDelete = "delete from Notice_Receiver where notice_id='" + mod.Notice_Id + "'";
                        db.ExecuteNonQuery(trans, CommandType.Text, sqlDelete);
                        
                        for (int i = 0; i < users.Count; i++)
                        {
                            MoNoticeReceiver modrec = new MoNoticeReceiver();
                            modrec.Notice_Id = mod.Notice_Id;
                            modrec.Rec_Status = 0;
                            modrec.Rec_Time = DateTime.Now;
                            modrec.Uid = users[i];

                            dalrec.CreateNoticeReceive(modrec, trans);
                        }
                        trans.Commit();
                        rc = true;
                        cnn.Close();
                    }
                }
                catch (DbException ex)
                {
                    if (trans != null) trans.Rollback();
                    HandleDbException(ex);
                }
            }
            return rc;
        }
    }
}

